package jazztures.plugins.alternativa3d {
	import alternativa.engine3d.containers.ConflictContainer;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.materials.FillMaterial;
	import alternativa.engine3d.materials.Material;
	import alternativa.engine3d.primitives.GeoSphere;

	import jazztures.geom.Point3D;
	import jazztures.objects.Skeleton;

	import flash.geom.Vector3D;
	
	public class Skeleton3D extends ConflictContainer
	{
		protected var material:Material;
		
		protected var head3D:Object3D;
		protected var neck3D:Object3D;
		protected var torso3D:Object3D;
		
		protected var leftShoulder3D:Object3D;
		protected var leftElbow3D:Object3D;
		protected var leftHand3D:Object3D;
		
		protected var rightShoulder3D:Object3D;
		protected var rightElbow3D:Object3D;
		protected var rightHand3D:Object3D;
		
		protected var leftHip3D:Object3D;
		protected var leftKnee3D:Object3D;
		protected var leftFoot3D:Object3D;
		
		protected var rightHip3D:Object3D;
		protected var rightKnee3D:Object3D;
		protected var rightFoot3D:Object3D;
		
		public var head:Point3D;
		public var neck:Point3D = new Point3D(0, -400, 0);
		
		public var leftShoulder:Point3D = new Point3D(-200, -300, 0);
		public var leftElbow:Point3D = new Point3D(-200, -25, 0);
		public var leftHand:Point3D = new Point3D(-200, 25, 0);
		
		public var rightShoulder:Point3D = new Point3D(200, -250, 0);
		public var rightElbow:Point3D = new Point3D(200, -25, 0);
		public var rightHand:Point3D = new Point3D(200, 25, 0);
		
		public var torso:Point3D = new Point3D(0, 0, 0);
		
		public var leftHip:Point3D = new Point3D(-25, 25 , 0);
		public var leftKnee:Point3D = new Point3D(-25, 300 , 0);
		public var leftFoot:Point3D = new Point3D(-25, 250 , 0);
		
		public var rightHip:Point3D = new Point3D(25, 25 , 0);
		public var rightKnee:Point3D = new Point3D(25, 300 , 0);
		public var rightFoot:Point3D = new Point3D(25, 250 , 0);

		public function Skeleton3D()
		{
			super();
			build();
		}
		
		protected function build():void{
			material = new FillMaterial(0xFF0000, 1, 1, 0x000000);
			
			head3D = getDefaultJoint();
			addChild(head3D);
			
			neck3D = getDefaultJoint();
			addChild(neck3D);
			
			torso3D = getDefaultJoint();
			addChild(torso3D);
			
			leftShoulder3D = getDefaultJoint();
			addChild(leftShoulder3D);
			
			leftElbow3D = getDefaultJoint();
			addChild(leftElbow3D);
			
			leftHand3D = getDefaultJoint();
			addChild(leftHand3D);
			
			rightShoulder3D = getDefaultJoint();
			addChild(rightShoulder3D);
			
			rightElbow3D = getDefaultJoint();
			addChild(rightElbow3D);
			
			rightHand3D = getDefaultJoint();
			addChild(rightHand3D);
			
			leftHip3D = getDefaultJoint();
			addChild(leftHip3D);
			
			leftKnee3D = getDefaultJoint();
			addChild(leftKnee3D);
			
			leftFoot3D = getDefaultJoint();
			addChild(leftFoot3D);
			
			rightHip3D = getDefaultJoint();
			addChild(rightHip3D);
			
			rightKnee3D = getDefaultJoint();
			addChild(rightKnee3D);
			
			rightFoot3D = getDefaultJoint();
			addChild(rightFoot3D);
		}
		
		protected function getDefaultJoint() : Object3D{
			var sphere:GeoSphere = new GeoSphere(20, 1,false, material);
			return sphere;
		}
		
		public function set skeleton(skeleton:Skeleton):void{
			head = skeleton.head;
			neck = skeleton.neck;
			torso = skeleton.torso;
			
			leftShoulder = skeleton.leftShoulder;
			leftElbow = skeleton.leftElbow;
			leftHand = skeleton.leftHand;
			
			rightShoulder = skeleton.rightShoulder;
			rightElbow = skeleton.rightElbow;
			rightHand = skeleton.rightHand;
			
			leftHip = skeleton.leftHip;
			leftKnee = skeleton.leftKnee;
			leftFoot = skeleton.leftFoot;
			
			rightHip = skeleton.rightHip;
			rightKnee = skeleton.rightKnee;
			rightFoot = skeleton.rightFoot;
		}
		
		public function update():void{
			setPositionAndRotation(head3D , head);
			setPositionAndRotation(neck3D, neck);
			setPositionAndRotation(torso3D, torso);
			
			setPositionAndRotation(leftShoulder3D,  leftShoulder);
			setPositionAndRotation(leftElbow3D,  leftElbow);			
			setPositionAndRotation(leftHand3D,  leftHand);
			
			setPositionAndRotation(rightShoulder3D,  rightShoulder);
			setPositionAndRotation(rightElbow3D,  rightElbow);			
			setPositionAndRotation(rightHand3D,  rightHand);
			
			setPositionAndRotation(leftHip3D,  leftHip);
			setPositionAndRotation(leftKnee3D,  leftKnee);
			setPositionAndRotation(leftFoot3D,  leftFoot);
			
			setPositionAndRotation(rightHip3D,  rightHip);
			setPositionAndRotation(rightKnee3D,  rightKnee);
			setPositionAndRotation(rightFoot3D,  rightFoot);
		}
		
		protected function setPositionAndRotation(obj:Object3D, source:Point3D):void{
			if(!obj) return;
			if(!source){
				obj.visible = false;
				return;
			}
			obj.visible = true;
			setRotation(obj, source);
			setPosition(obj, source.vector3D);
		}
		
		protected function setPosition(obj:Object3D, source:Vector3D):void{
			obj.x = source.x;
			obj.y = source.y;
			obj.z = source.z;
		}
		
		protected function setRotation(obj:Object3D, source:Point3D):void{
			obj.rotationX = source.rotationX;
			obj.rotationY = source.rotationY;
			obj.rotationZ = source.rotationZ;
		}
	}
}